package cn.stronglink.assetmanage.business.setting.synctype;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.setting.synccontent.SyncListener;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.entity.SyncTypes;

/**
 * @author zww
 * 2018 2018/6/18 14:43
 */
public class SystemSyncTypeAdapter extends RecyclerView.Adapter<SystemSyncTypeHolder> {
    public List<SyncTypes> list = new ArrayList<>();
    private Context mContext;
    private SyncListener listener;

    SystemSyncTypeAdapter(Context context) {
        this.mContext = context;
        if (DaoManager.getDaoSession(context).getSyncTypesDao().loadAll().size() == 0) {
            initData();
            for (SyncTypes types :
                    list) {
                DaoManager.getDaoSession(context).getSyncTypesDao().insert(types);
            }
        } else {
            list = DaoManager.getDaoSession(context).getSyncTypesDao().loadAll();
        }
    }

    private void initData() {
        SyncTypes types1 = new SyncTypes();
        types1.setImgCheck(R.drawable.checked);
        types1.setImgUnCheck(R.drawable.uncheck);
        types1.setIsSelected(false);
        types1.setSyscTs(1);
        types1.setSysText("自动同步");
        list.add(types1);

        //默认是离线同步
        SyncTypes types2 = new SyncTypes();
        types2.setImgCheck(R.drawable.checked);
        types2.setImgUnCheck(R.drawable.uncheck);
        types2.setIsSelected(true);
        types2.setSyscTs(2);
        types2.setSysText("离线同步");
        list.add(types2);
    }

    @Override
    public SystemSyncTypeHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View inflate = View.inflate(mContext, R.layout.item_system_synctype, null);
        return new SystemSyncTypeHolder(inflate, listener);
    }

    @Override
    public void onBindViewHolder(SystemSyncTypeHolder holder, int position) {
        if (null != list) {
            if (list.get(position).getIsSelected()) {
                holder.txtSyncTypeContent.setText(list.get(position).getSysText());
                holder.imgSyncTypeCheck.setImageResource(list.get(position).getImgCheck());
                holder.itemView.setBackgroundResource(R.drawable.shape_corner);
                holder.txtSyncTypeContent.setTextColor(mContext.getResources().getColor(R.color.colorWhite));
            } else {
                holder.txtSyncTypeContent.setText(list.get(position).getSysText());
                holder.imgSyncTypeCheck.setImageResource(list.get(position).getImgUnCheck());
                holder.itemView.setBackgroundResource(R.drawable.shape_corner_default);
                holder.txtSyncTypeContent.setTextColor(mContext.getResources().getColor(R.color.blue));
            }
        }
    }

    @Override
    public int getItemCount() {
        if (list!=null) {
            return list.size();
        }
        return 0;
    }

    public void setOnItemClickListener(SyncListener listener) {
        this.listener = listener;
    }
}
